package Server.ServerCore.Controller;

import Server.ExtComponents.BaseConfig.MServerConfig;
import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Service.DataPacks.packRequest.ExchangeChips_request;
import Server.ServerCore.Service.DataPacks.packRespose.ExchangeChips_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.service.PlayerManagerService;
import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-24
 *         Time: 下午7:16
 */
@Service
public class ExchangeController extends HandleController {
    /**
     * 数据分解具体处理的函数
     *
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {
        ExchangeChips_request exchangeChipsRequest= (ExchangeChips_request) parseObject(buffer,ExchangeChips_request.class);
        double chips=exchangeChipsRequest.getChips();
        double interation=chips/ MServerConfig.ExchangeChips;//积分
        //兑换积分处理结果
        ExchangeChips_response exchangeChipsResponse=new ExchangeChips_response();
        byte result=2;
        if (interation>player.getRole_Integration()){//积分不足
            result=1;//积分不足
            exchangeChipsResponse.setChips(player.getBargainingChip());
            exchangeChipsResponse.setInteration(player.getRole_Integration());
        }else {
            //更新数据库，返回处理结果
            player.setRole_Integration(player.getRole_Integration()-interation);
            player.setBargainingChip(player.getBargainingChip()+chips);
            player.getGameRole().setRoleBargainingChip(player.getBargainingChip());
            player.getGameRole().setRoleIntegration(player.getRole_Integration());
            PlayerManagerService.update(player.getGameRole());

            exchangeChipsResponse.setChips(player.getBargainingChip());
            exchangeChipsResponse.setInteration(player.getRole_Integration());
            result=0;
        }
        exchangeChipsResponse.setResult(result);
        player.DirectSend(exchangeChipsResponse.getJsonData(4,1));

    }

    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[41]=this;
    }
}
